package com.bodu.global.config;

import cn.hutool.core.util.ObjectUtil;
import com.bodu.global.security.LoginUser;
import com.bodu.system.entity.po.AdminPo;
import com.bodu.system.entity.po.PermitPo;
import com.bodu.system.entity.po.RolePo;
import com.bodu.system.service.AdminService;
import com.bodu.system.service.PermitService;
import com.bodu.system.service.RoleService;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 登录用户详情
 *
 * @author Administrator
 */
@Service("userDetailsService")
public class SecurityUserDetailServiceImpl implements UserDetailsService {
    private final AdminService adminService;
    private final RoleService roleService;
    private final PermitService permitService;

    public SecurityUserDetailServiceImpl(AdminService adminService, RoleService roleService, PermitService permitService) {
        this.adminService = adminService;
        this.roleService = roleService;
        this.permitService = permitService;
    }

    @Override
    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
        AdminPo admin = adminService.getModelByUserName(username);
        if (ObjectUtil.isEmpty(admin)) {
            return null;
        }
        List<RolePo> roles = roleService.listByAdminId(admin.getId());
        List<PermitPo> permits = permitService.listByAdminId(admin.getId());
        return new LoginUser(admin.getId(), username, admin.getPass(),admin.getUrl(), roles, permits);
    }
}
